﻿using QiuShiBaiKe.BLL;
using QiuShiBaiKe.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QiuShiBaiKe.Web.Controllers
{
    public class UserController : Controller
    {
        public ActionResult ShowLogin()
        {
            return View();
        }

        public ActionResult Login(FormCollection fc)
        {
            string userName = fc["UserName"];
            string password = fc["Password"];
            string validCode = fc["ValidCode"];
            if(string.IsNullOrWhiteSpace(userName))
            {
                return WebHelper.GetValidationMsg_Empty("用户名");
            }
            if(string.IsNullOrWhiteSpace(password))
            {
                return WebHelper.GetValidationMsg_Empty("密码");
            }
            if(string.IsNullOrWhiteSpace(validCode))
            {
                return WebHelper.GetValidationMsg_Empty("验证码");
            }

            if (!WebHelper.IsValidCodeOK(HttpContext,validCode))
            {
                WebHelper.ResetValidCode(HttpContext);
                return WebHelper.GetJsonResult(false, "验证码不正确！");
            }
            UserBLL userBLL = new UserBLL(WebHelper.DbContext);
            bool isOK = userBLL.CheckLogin(userName, password);
            if (isOK)
            {
                Model.User user = userBLL.GetByUserName(userName);
                WebHelper.SaveLoginedUser(HttpContext, user);
            }
            string destUrl = WebHelper.GetLoginDestinationUrl(HttpContext);
            return WebHelper.GetJsonResult(isOK, "用户名或者密码错误", destUrl);
        }

        public ActionResult ShowRegister()
        {
            return View();
        }

        public ActionResult Register(FormCollection fc)
        {
            string userName = fc["userName"];
            string password = fc["password"];
            string validCode = fc["validCode"];
            if (string.IsNullOrWhiteSpace(userName))
            {
                return WebHelper.GetValidationMsg_Empty("用户名");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                return WebHelper.GetValidationMsg_Empty("密码");
            }
            if (string.IsNullOrWhiteSpace(validCode))
            {
                return WebHelper.GetValidationMsg_Empty("验证码");
            }
            if (!WebHelper.IsValidCodeOK(HttpContext,validCode))
            {
                return WebHelper.GetJsonResult(false, "验证码不正确！");
            }
            UserBLL userBLL = new UserBLL(WebHelper.DbContext);
            AddUserResult addUserResult = userBLL.Add(userName, password);
            switch (addUserResult)
            {
                case AddUserResult.OK:
                    return WebHelper.GetJsonResult(true, "注册成功", "/Home/Index");
                case AddUserResult.UserNameBadWords:
                    return WebHelper.GetJsonResult(false, "用户名中包含禁用词，请修改后再注册");
                case AddUserResult.UserNameExists:
                    return WebHelper.GetJsonResult(false, "您注册的用户名已经被占用，请修改后再注册");
                case AddUserResult.UserNameLengthInvalid:
                    return WebHelper.GetJsonResult(false, "用户名的长度必须在3至15之间");
                default:
                    return WebHelper.GetJsonResult(false, "未知错误");
            }
        }

        public ActionResult CheckUserName(FormCollection fc)
        {
            string userName = fc["userName"];
            if (string.IsNullOrWhiteSpace(userName))
            {
                return WebHelper.GetValidationMsg_Empty("用户名");
            }

            bool isExists = new UserBLL(WebHelper.DbContext).IsExists(userName);
            return WebHelper.GetJsonResult(!isExists, isExists?"此用户名已经被占用":"");
        }

        public ActionResult CheckLogin()
        {
            var user = WebHelper.GetLoginedUser(HttpContext);
            if (user == null)
            {
                return WebHelper.GetJsonResult(false, "");
            }
            else
            {
                user.Messages = null;
                return WebHelper.GetJsonResult(true, "", "", new {UserId=user.Id,UserName=user.UserName });
            }
        }

        public ActionResult ValidCode()
        {
            string validCode;
            byte[] buffer = WebHelper.CreateValidCode(out validCode);
            WebHelper.SaveValidCode(HttpContext, validCode);
            return File(buffer, "image/jpeg");
        }
    }
}
